package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import vo.DogBean;

public class DogDao {
	Connection conn;
	public DogDao() {
		DBConnect con = new DBConnect();
		conn = con.getConnection();
	}//const
	
	public ArrayList<DogBean> getList() {
		ArrayList<DogBean> list = new ArrayList<DogBean>();
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		String sql = "select * from dog";
		try {
			pstmt = conn.prepareStatement(sql);
			rs = pstmt.executeQuery();
			while(rs.next()){
				DogBean bean = new DogBean();
				bean.setId(rs.getInt(1));
				bean.setKind(rs.getString(2));
				bean.setPrice(rs.getInt(3));
				bean.setImage(rs.getString(4));
				bean.setCountry(rs.getString(5));
				bean.setHeight(rs.getInt(6));
				bean.setWeight(rs.getInt(7));
				bean.setContent(rs.getString(8));
				bean.setReadcount(rs.getInt(9));
				list.add(bean);
			}//while
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {rs.close();} catch (SQLException e) {}
			try {pstmt.close();} catch (SQLException e) {}
			try {conn.close();} catch (SQLException e) {}
		}// try~finally
		return list;
	}//getList()

	public DogBean getDog(String id) {
		DogBean bean = new DogBean();
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		String sql = "select * from dog where id = ?";
		try {
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, id);
			rs = pstmt.executeQuery();
			while(rs.next()){
				bean.setId(rs.getInt(1));
				bean.setKind(rs.getString(2));
				bean.setPrice(rs.getInt(3));
				bean.setImage(rs.getString(4));
				bean.setCountry(rs.getString(5));
				bean.setHeight(rs.getInt(6));
				bean.setWeight(rs.getInt(7));
				bean.setContent(rs.getString(8));
				bean.setReadcount(rs.getInt(9));
			}//while
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {rs.close();} catch (SQLException e) {}
			try {pstmt.close();} catch (SQLException e) {}
			try {conn.close();} catch (SQLException e) {}
		}// try~finally
		return bean;
	}//getDog()

	public boolean readCntUp(String id) {
		boolean flag = false;
		PreparedStatement pstmt = null;
		String sql = "update dog set readcount = readcount + 1 where id = ?";
		try {
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, id);
			int sel = pstmt.executeUpdate();
			if(sel > 0){
				flag = true;
			}//if
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {pstmt.close();} catch (SQLException e) {}
			try {conn.close();} catch (SQLException e) {}
		}// try~finally
		return flag;
	}//readCntUp()

	public boolean getLogin(String id, String pw) {
		boolean flag = false;
		Statement stmt = null;
		ResultSet rs = null;
		String sql = "select count(*) from dogmem "
				+ "where id = '"+id+"' and pw = '"+pw+"'";
		try {
			stmt = conn.createStatement();
			rs = stmt.executeQuery(sql);
			while(rs.next()){
				if(rs.getInt(1)>0){
					flag = true;
				}//if
			}//while
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {rs.close();} catch (SQLException e){}
			try {stmt.close();} catch (SQLException e){}
			try {conn.close();} catch (SQLException e){}
		}//try~finally
		return flag;
	}//getLogin()
	
	
	
	
}//class
